import Vue from "vue";
import VueRouter from "vue-router";
import login from "@/components/Login";

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: "/login"
  },
  {
    path: "/login",
    name: "login", component: login
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/welcome',
    component: () => import('@/components/home'),
    children: [
      {
        path: '/users',
        name: 'users',
        component: () => import('@/components/children/users'),
      },
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('@/components/children/welcome'),
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('@/components/children/roles'),
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('@/components/children/rights'),
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('@/components/children/goods'),
      },
      {
        path: '/goods/add',
        name: 'add',
        component: () => import('@/components/children/children/add'),
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('@/components/children/params'),
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('@/components/children/categories'),
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('@/components/children/orders'),
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('@/components/children/reports'),
      },
    ]
  },
];

const router = new VueRouter({
  routes
});
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const Token = window.sessionStorage.getItem('token')
  if (!Token) return next('/login')
  next()
})
export default router;
